﻿using OpenTK.Graphics.OpenGL;

namespace OpenGl.Explore.FirstApp
{
    public class ShaderObject : OpenglObject
    {
        public ShaderObject()
        {

        }

        public void Compile()
        {
            GL.CompileShader(_id);
            GL.GetShader(_id, ShaderParameter.CompileStatus, out int success);
            if (success == 0)
            {
                GL.GetShaderInfoLog(_id, out string infoLog);
                throw new Exception($"{this.GetType().Name} Compile Failed: {infoLog}");
            }
        }

        public void SetSource(string sourceCode)
        {
            GL.ShaderSource(_id, sourceCode);
        }

        protected override void Destory()
        {
            GL.DeleteShader(_id);
        }

        protected void Init(string file)
        {
            using (StreamReader sr = new StreamReader(file))
            {
                string source = sr.ReadToEnd();
                SetSource(source);
            }
        }
    }
}
